package com.mouse.dnf.service;

import com.mouse.damo.micro.*;
import com.mouse.damo.service.CustomService;
import com.mouse.damo.service.Point;
import com.mouse.damo.util.StringUtil;
import com.mouse.dnf.DnfApplication;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;

public class ChanDouService {
    //TODO
    private static String appName = "DNF Taiwan";

    private static Com grobalCom = new Com();
    private static Window window = new Window(grobalCom);
    private CustomService customService = new CustomService();

    private boolean start = false;

    public void setStart(boolean start) {
        this.start = start;
    }

    public Com findHwnd() {
        String windows = window.findWindows(appName);
        if (StringUtil.isBlank(windows)) return null;
        String hwndStr = Stream.of(windows.split(",")).filter(s -> window.getWindowClass(Integer.valueOf(s))
                .equalsIgnoreCase(appName)).findFirst().orElse(null);
        if (StringUtil.isBlank(windows)) return null;
        Com bingCom = new Com();
//        File dnfResource = new File("dnfResource");
        File dnfResource = new File("D:\\dnf");
        bingCom.setResourcePath(dnfResource.getAbsolutePath());
        boolean bind = bingCom.bind(Integer.valueOf(hwndStr));
        DnfApplication.setLog("绑定结果:" + bind);
        return bingCom;
    }

    /**
     * @param com
     * @return
     */
    public int auto(Com com) {
        while (true) {
            Sleep.sleep(3000);
            if (!start) continue;
            Window window = new Window(com);
            int[] clientSize = window.getClientSize(com.getHwnd());
            //检查疲劳
//            checkPiLao(com, clientSize);
            //进图
            boolean wangXiaZou = wangXiaZou(com, clientSize);
            if (!wangXiaZou) continue;
//            boolean jinTuFlag = jinTu(com, clientSize);
            boolean jinTuFlag = xuanTu(com, clientSize);
            if (!jinTuFlag) continue;
            //循环打怪
            daGuai(com, clientSize);
        }
    }

    private boolean wangXiaZou(Com com, int[] clientSize) {
        int width = clientSize[1], height = clientSize[2];
        Mouse mouse = new Mouse(grobalCom);
        FindPic findPic = new FindPic(com);
        Point piLaoPoint = customService.loopPoint(2, () -> new Point(findPic.findPic(0, 0, width, height,
                "城中.bmp", "203040", 0.9, 0)));
        if (!piLaoPoint.isSuccess()) {
            DnfApplication.setLog("不在城中");
            return false;
        }
        window.setWindowActivate(com.getHwnd());
        Sleep.sleep(500);
        Press press = new Press(grobalCom);
        press.keyPressTime(Press.DOWN, 3000);
        return true;
    }

    private boolean xuanTu(Com com, int[] clientSize) {
        Mouse mouse = new Mouse(com);
        FindPic findPic = new FindPic(com);
        int width = clientSize[1], height = clientSize[2];

        Point tuPoint = customService.loopPoint(5, () -> new Point(findPic.findPic(0, 0, width, height,
                "大地.bmp|大地2.bmp", "203040", 0.9, 0)));
        if (!tuPoint.isSuccess()) return false;
        mouse.mouseClick(tuPoint.getX(), tuPoint.getY(), true);
        Sleep.sleep(300);
        mouse.mouseClick(tuPoint.getX(), tuPoint.getY(), true);
        //判断是否进图 TODO
        DnfApplication.setLog("判断是否在图里");
        boolean checkFlag = checkInTu(com, width, height);
        if (!checkFlag) return false;
        return true;
    }

    public boolean checkHwnd(Integer hwnd) {
        Window window = new Window(grobalCom);
        return window.windowIsExist(hwnd);
    }

    private void checkPiLao(Com com, int[] clientSize) {
        //检查疲劳
        FindPic findPic = new FindPic(com);
        Mouse mouse = new Mouse(com);
        //空疲劳
        DnfApplication.setLog("检查疲劳");
        Point piLaoPoint = customService.loopPoint(2, () -> new Point(findPic.findPic(0, 0, clientSize[1], clientSize[2],
                "piLao.bmp", "203040", 0.9, 0)));
        if (piLaoPoint.isSuccess()) {
            //点击数字6
            mouse.mouseClick(piLaoPoint.getX() - 100, piLaoPoint.getY() - 20, false);
            Sleep.sleep(100);
        }
    }

    /**
     * @param com
     * @param clientSize
     * @return 0.错误 1.暂停
     */
    private int daGuai(Com com, int[] clientSize) {
        Mouse mouse = new Mouse(com);
        int width = clientSize[1], height = clientSize[2];
        FindPic findPic = new FindPic(com);
        setActive(com);
        Sleep.sleep(500); //TODO
        Press press = new Press(grobalCom);
        press.keyPress(Press.T);//二觉
        Sleep.sleep(3000); //TODO
        //打右图
        DnfApplication.setLog("打右图");
        shunTu(com, press, Press.RIGHT,width,height);
        Point youPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                "右图.bmp", "203040", 0.9, 0)));
        if (!youPoint.isSuccess()) {
            //huiCheng(com, clientSize);
            return 0;
        }
        setActive(com);
        press.keyPress(Press.T);//二觉
        Point youPoint2 = customService.loopPoint(15, () -> new Point(findPic.findPic(0, 0, width, height,
                "右图1.bmp", "203040", 0.9, 0)));
        if (!youPoint2.isSuccess()) {
            //huiCheng(com, clientSize);
            return 0;
        }
        DnfApplication.setLog("回中图");
        //回中图
        shunTu(com, press, Press.LEFT, width, height);
        //打左图
        DnfApplication.setLog("打左图");
        Sleep.sleep(1000); //TODO
        shunTu(com, press, Press.LEFT, width, height);
        Point zuoPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                "左图.bmp", "203040", 0.9, 0)));
        if (!zuoPoint.isSuccess()) {
            //huiCheng(com, clientSize);
            return 0;
        }
        setActive(com);
        press.keyPressTime(Press.UP, 200);
        press.keyPressTime(Press.LEFT, 1800);
        Sleep.sleep(500); //TODO
        press.keyPress(Press.TAB);//佣兵
        Sleep.sleep(200); //TODO
        press.keyPress(Press.Q);
        Sleep.sleep(800); //TODO
        press.keyPress(Press.X);
        Point zuoTuPoint2 = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                "左图2.bmp", "203040", 0.9, 0)));
        if (!zuoTuPoint2.isSuccess()) {
            //huiCheng(com, clientSize);
            return 0;
        }
        //回中图
        DnfApplication.setLog("回中图");
        shunTu(com, press, Press.RIGHT, width, height);
        //进最后图
        DnfApplication.setLog("进最后图");
        Sleep.sleep(2000); //TODO
        shunTu(com, press, Press.UP, width, height);
        //翻牌子
        Point fanPaiPoint = customService.loopPoint(20, () -> new Point(findPic.findPic(0, 0, width, height,
                "fanpai.bmp", "203040", 0.9, 0)));
        if (!fanPaiPoint.isSuccess()) {
            //huiCheng(com, clientSize);
            return 0;
        }
        DnfApplication.setLog("翻牌子");
        List<Integer> paiList = Arrays.asList(200, 400, 600, 800);//牌子
        Integer paiX = paiList.get(new Random().nextInt(4));
        mouse.mouseClick(paiX, fanPaiPoint.getY() + 100, true);
        //再打一次图
        DnfApplication.setLog("再打一次图");
        Point againPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                "again.bmp", "203040", 0.9, 0)));
        if (!againPoint.isSuccess()) {
            huiCheng(com, clientSize);
            return 0;
        }
        if (!start) return 1;
        mouse.mouseClick(againPoint.getX(), againPoint.getY(), true);
        checkInTu(com, width, height);
        return 1;
    }


    private void setActive(Com com) {
        window.setWindowActivate(com.getHwnd());
        Sleep.sleep(100);
    }

    //回城
    private boolean huiCheng(Com com, int[] clientSize) {
        Press press = new Press(grobalCom);
        Mouse mouse = new Mouse(com);
        int width = clientSize[1], height = clientSize[2];
        FindPic findPic = new FindPic(com);
        for (int i = 0; i < 3; i++) {
            window.setWindowActivate(com.getHwnd());
            Sleep.sleep(100);
            press.keyPress(Press.ESC);
            //TODO
            Point youPoint = customService.loopPoint(10, () -> new Point(findPic.findPic(0, 0, width, height,
                    "huiCheng.bmp", "203040", 0.9, 0)));
            if (youPoint.isSuccess()) {
                mouse.mouseClick(youPoint.getX(), youPoint.getY(), true);
                return true;
            }
        }
        DnfApplication.setLog("回城失败");
        return false;
    }

    //进图
    private boolean jinTu(Com com, int[] clientSize) {
        int width = clientSize[1], height = clientSize[2];
        FindPic findPic = new FindPic(com);
        DnfApplication.setLog("鼠标进图");
        //找进图标识 TODO
        Point point = customService.loopPoint(15, () -> new Point(findPic.findPic(0, 0, width, height,
                "men.png", "203040", 0.9, 0)));
        if (!point.isSuccess()) return false;
        Mouse mouse = new Mouse(com);
        DnfApplication.setLog("选图");
        //TODO 点击进图
        mouse.mouseClick(point.getX() + 5, point.getY() + 10, false);
        Point tuPoint = customService.loopPoint(5, () -> new Point(findPic.findPic(0, 0, width, height,
                "tu.png", "203040", 0.9, 0)));
        if (!tuPoint.isSuccess()) return false;
        //TODO
        mouse.mouseClick(tuPoint.getX() + 0, tuPoint.getY() + 0, true);
        //判断是否进图 TODO
        DnfApplication.setLog("判断是否在图里");
        boolean checkFlag = checkInTu(com, width, height);
        if (!checkFlag) return false;
        return true;
    }

    private boolean checkInTu(Com com, int width, int height) {
        FindPic findPic = new FindPic(com);
        Point inTuPoint = customService.loopPoint(20, () -> new Point(findPic.findPic(width-300, 0, width, 300,
                "在图.bmp|在图2.bmp", "203040", 0.9, 0)));
        if (!inTuPoint.isSuccess()) return false;
        return true;
    }

    private void shunTu(Com com, Press press, int keyCode, int width, int height) {
        for (int i = 0; i < 3; i++) {
            setActive(com);
            Sleep.sleep(300);
            press.groupPress(Press.ALT, keyCode);
        }
    }
}
